﻿@using System.Collections.Concurrent;
@inject IStringLocalizer<CalendarCellTemplateDemo> Localizer

<Calendar @bind-Value="CrewInfoValue">
    <CellTemplate>
        <td class="@context.DefaultCss">
            <CalendarCrewCell @bind-Value="@context" Crews="GetCrewsByDate(context.CellValue)" />
        </td>
    </CellTemplate>
</Calendar>
<div class="mt-2 sum">
    <b>@Localizer["CellTemplateDemoSummary"]</b>
    <div class="row g-2 mt-1">
        @foreach (var crew in CalendarDemoDataHelper.Crews)
        {
            <div class="col-12">
                <div>@crew.Name</div>
                <div>@GetSumByName(crew.Name)</div>
            </div>
        }
    </div>
</div>

@code {
    private ConcurrentDictionary<DateTime, List<Crew>> Data { get; } = new();

    private DateTime CrewInfoValue { get; set; } = DateTime.Today;

    private List<Crew> GetCrewsByDate(DateTime d) => Data.GetOrAdd(d, CalendarDemoDataHelper.GetCrewsByDate);

    private int GetSumByName(string name) => Data.Where(d => d.Key.Month == CrewInfoValue.Month).Sum(d => d.Value.FirstOrDefault(v => v.Name == name)?.Value ?? 0);
}
